package com.jeremy.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jeremy.entity.User;
import com.jeremy.service.UserService;

@Controller(value="userController")
public class UserController {
	
	@Autowired
	private UserService service;
	
	@RequestMapping("/index")
	public String index(Model m,@RequestParam(defaultValue="1",required=true,value="pageNo")Integer pageNo) {
		//ModelAndView的实例是需要我们手动new，Model可以通过参数传过来！！！！！
		//ModelAndView mav = new ModelAndView();
		//设置视图：mav.setViewName("xxx");或者new ModelAndView("xxx");
		Integer pageSize=5;//每页显示记录数
		
		//分页查询
        PageHelper.startPage(pageNo, pageSize);
        List<User> userList = service.findAll();//获取所有用户信息
        PageInfo<User> pageInfo=new PageInfo<User>(userList);
		m.addAttribute("pageInfo", pageInfo);
		return "index";
	}
	
	@RequestMapping("/editUser")
	public String editUser(Model m,User user)
	{
		User u = service.findOne(user.getId());
		m.addAttribute("user", u);
		return "editUser";
	}
	@RequestMapping("/updateUser")
	public String updateUser(Model m,User user)
	{
		service.updateUser(user);
		return "redirect:index";
	}
	
	@RequestMapping("/addPage")
	public String addPage(Model m,User user)
	{
		
		return "addPage";
	}
	@RequestMapping("/addUser")
	public String addUser(Model m,User user)
	{
		service.addUser(user);
		return "redirect:index";
	}
	@RequestMapping("/deleteUser")
	public String deleteUser(Model m,User user)
	{
		service.deleteUser(user.getId());
		return "redirect:index";
	}
}
